package com.dreamer.service.count;

import com.dreamer.domain.mall.goods.GoodsTransferStatus;
import com.dreamer.domain.mall.transfer.Transfer;
import com.dreamer.domain.user.Agent;
import com.dreamer.domain.user.AgentLevelName;
import com.dreamer.domain.user.MutedUser;
import com.dreamer.service.mobile.AgentHandler;
import com.dreamer.service.mobile.MuteUserHandler;
import com.dreamer.service.mobile.TransferHandler;
import com.dreamer.util.CommonUtil;
import com.wxjssdk.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class CountHandlerImpl implements CountHandler {

    @Override
    public void calculateFund(HttpServletResponse response, String startTime, String endTime) {
        Date startDate = DateUtil.formatStartTime(startTime);
        Date endDate = DateUtil.formatEndTime(endTime);
        MutedUser mutedUser = muteUserHandler.getMuteUser();
        //找到公司
        List<Transfer> transfers = transferHandler.findFromOrToTransfer(startDate, endDate, mutedUser.getId(), GoodsTransferStatus.CONFIRM);
        //总账
        HashMap<Agent, Double> sumAd = new HashMap<>();
        //记录
        HashMap<Agent, String> sumS = new HashMap<>();
        for (Transfer transfer : transfers) {
            System.out.println(transfer.getToAgent().getRealName()+"+++++" + transfer.getUpdateTime());
            //给补贴
            if (transfer.getRemittance().equals("订单已成交")) {
                HashMap<Agent, Double> tem = new HashMap<>();
                HashMap<Agent, Double> map = transferHandler.getAgentsWithVoucher(transfer.getToAgent(), transfer.getItems());
                map.keySet().forEach(agent -> {
                    String ln = agentHandler.getLevelName(agent);
//                    if(ln.equals(AgentLevelName.分公司.toString())||ln.equals(AgentLevelName.联盟单位.toString())||ln.equals(AgentLevelName.独立核算.toString())){
//                    if(ln.equals(AgentLevelName.分公司.toString())||ln.equals(AgentLevelName.联盟单位.toString())||ln.equals(AgentLevelName.独立核算.toString())||ln.contains("董")){
                    if (true) {
                        tem.put(agent, map.get(agent));
                        StringBuffer sb = new StringBuffer();
                        transfer.getItems().forEach(item -> {
                            sb.append(item.getGoods().getName() + "X" + item.getQuantity() + "  ");
                        });
                        String more = transfer.getToAgent().getRealName() + transfer.getToAgent().getAgentCode() + "购买" + sb.toString() + "共补贴" + map.get(agent);
                        if (sumS.containsKey(agent)) {
                            sumS.put(agent, sumS.get(agent) + "  " + more);
                        } else {
                            sumS.put(agent, more);
                        }
                    }
                });
                //非管理员与公司
                CommonUtil.putAll(sumAd, tem);
            }
            //退货退获得奖励
            if (transfer.getRemittance().equals("订单已退货!")) {
                HashMap<Agent, Double> tem = new HashMap<>();
                HashMap<Agent, Double> map = transferHandler.getAgentsWithVoucher(transfer.getFromAgent(), transfer.getItems());
                //退货扣减
                map.forEach((k, v) -> {
                    map.put(k, -v);
                });
                //筛选
                map.keySet().forEach(agent -> {
                    String ln = agentHandler.getLevelName(agent);
//                    if(ln.equals(AgentLevelName.分公司.toString())||ln.equals(AgentLevelName.联盟单位.toString())||ln.equals(AgentLevelName.独立核算.toString())){
//                    if(ln.equals(AgentLevelName.分公司.toString())||ln.equals(AgentLevelName.联盟单位.toString())||ln.equals(AgentLevelName.独立核算.toString())||ln.contains("董")){
                    if (true) {
                        tem.put(agent, map.get(agent));
                        StringBuffer sb = new StringBuffer();
                        transfer.getItems().forEach(item -> {
                            sb.append(item.getGoods().getName() + "X" + item.getQuantity() + "  ");
                        });
                        String more = transfer.getFromAgent().getRealName() + transfer.getFromAgent().getAgentCode() + "退回" + sb.toString() + "共扣除" + map.get(agent);
                        if (sumS.containsKey(agent)) {
                            sumS.put(agent, sumS.get(agent) + "  " + more);
                        } else {
                            sumS.put(agent, more);
                        }
                    }
                });
                CommonUtil.putAll(sumAd, tem);
            }
        }
        sumAd.forEach((k, v) -> {
            System.out.println(k.getRealName() + "--" + k.getAgentCode() + "--" + v);
        });

        sumS.forEach((k, v) -> {
            System.out.println(k.getRealName() + "--" + k.getAgentCode() + "--" + v);
        });
    }

    @Autowired
    private TransferHandler transferHandler;

    @Autowired
    private MuteUserHandler muteUserHandler;

    @Autowired
    private AgentHandler agentHandler;


}
